<?php
require_once( '../../libs/smarty.inc' );
require_once( '../../libs/smt_variables.php' );
require_once( '../../libs/Class_DB.php' );
require_once( '../../libs/Class_ERROR.php' );
require_once( '../../libs/login.inc.php' );
require_once( '../../libs/const.php' );
require_once( '../../libs/page.php' );

$current = 1 ;
if($_SERVER['REQUEST_METHOD'] == 'GET'){
	if( isset( $_GET['page'] ) && is_numeric( $_GET['page'] ) ){
		$current = $_GET['page'];
		$st = ($current -1) * PAGE_SIZE;
	}else{
		$current = 1;
		$st = 0;
	}

	$db = new Class_DB;
	
	$forms = $_GET;
	$consumer_id = $_GET["consumer_id"];
	$shop_name = $_GET["shop_name"];
	$type = $_GET["type"];
	$style = $_GET["style"];
	$num = $_GET["num"];
	$pro_name = $_GET["pro_name"];
	$refer_price1 = $_GET["refer_price1"];
	$refer_price2 = $_GET["refer_price2"];
	$r_date1 = $_GET["r_date1"];
	$r_date2 = $_GET["r_date2"];
	$u_date1 = $_GET["u_date1"];
	$u_date2 = $_GET["u_date2"];

	$sql ="SELECT * ";
	$sqlcount = "SELECT COUNT(*) as cnt ";
	$sqlstr = " FROM p_consumer_head a,p_consumer_detail b,p_shop c WHERE a.consumer_id=$consumer_id AND a.price_no = b.price_no AND b.shop_id = c.shop_id  ";
	
	if($shop_name != null){
		$sqlstr.=" AND c.shop_name like '%$shop_name%' ";
	}
	if($type != null){
		$sqlstr.=" AND c.type like '%$type%' ";
	}
	if($style != null){
		$sqlstr.=" AND c.style like '%$style%' ";
	}
	if($num != null){
		$sqlstr.=" AND b.quantity = $num ";
	}
	if($pro_name != null){
		$sqlstr.=" AND a.pro_name like '%$pro_name%' ";
	}
	if($refer_price1 != null && $refer_price2 != null){
		$sqlstr.=" AND c.refer_price*b.discount between $refer_price1 and $refer_price2 ";
	}
	if($refer_price1 != null && $refer_price2 == null){
		$sqlstr.=" AND c.refer_price*b.discount >= $refer_price1 ";
	}
	if($refer_price1 == null && $refer_price2 != null){
		$sqlstr.=" AND c.refer_price*b.discount <= $refer_price2 ";
	}
	if($r_date1 != null && $r_date2 != null){
		$sqlstr.=" AND a.period_str between '$r_date1' and '$r_date2' ";
	}
	if($r_date1 != null && $r_date2 == null){
		$sqlstr.=" AND a.period_str >= '$r_date1'";
	}
	if($r_date1 == null && $r_date2 != null){
		$sqlstr.=" AND a.period_str <= '$r_date2' ";
	}
	if($u_date1 != null && $u_date2 != null){
		$sqlstr.=" AND a.period_end between '$u_date1' and '$u_date2' ";
	}
	if($u_date1 != null && $u_date2 == null){
		$sqlstr.=" AND a.period_end >= '$u_date1'";
	}
	if($u_date1 == null && $u_date2 != null){
		$sqlstr.=" AND a.period_end <= '$u_date2' ";
	}

	$sql =  $sql.$sqlstr." order by b.did desc LIMIT $st, ".PAGE_SIZE;
	$db = new Class_DB;
	
	$head = "件名,型番,商品名,仕様,数量,単価,参考価格,掛け率,特価理由,適用期間（開始）,適用期間（終了）";
	
	$db->query( $sql );
	
	$data = array();
	
	while( $row = $db->fetch() ){
		$row['money'] = $row['refer_price']*$row['discount']*$row['quantity'];
		$row['profit'] = round((($row['money']-$row['cost_price']*$row['quantity'])/$row['money'])*100,2);
		
		$data[] = mb_convert_encoding( sprintf("\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"",
				$row['pro_name'],
				$row['type'],
				$row['shop_name'],
				$row['style'],
				$row['quantity'],
				$row['refer_price']*$row['discount'],
				$row['refer_price'],
				$row['discount'],
				$row['special_reason'],
				$row['period_str'],
				$row['period_end']
		),
				"sjis",
				"utf8"
		);
	}
	
	$filename = date("YmdHis") . '.csv';
	
	$head = mb_convert_encoding( $head, "sjis", "utf8");
	
	
	header("Content-Type: application/octet-stream");
	header('Content-Disposition: attachment; filename=' . $filename );
	
	print $head;
	print "\n";
	print implode( $data, "\n" );
	

}

/*
$forms = $_GET;
$db = new Class_DB;

$sql = "SELECT * FROM p_shop S,p_consumer_detail P where S.shop_id = P.shop_id";


//$head = "は引用、顧客ID、作成日、更新日、タイトル、価格、数量、合計金額、倍率、ユニットの種類、生産者、承認、承認日、顧客が同意し、会社名、営業担当者、担当、支払";
$head = "shop_id,type,shop_name,style,refer_price,cost_price,multiple,num,count,s_unit,r_date,u_date,applicant,remark,special_reason,price_no,consumer_id,m_date,pro_name,category,producer,examiner,e_date,status,staff_id";

$db->query( $sql );

$data = array();

while( $row = $db->fetch() ){	

	
	$data[] = mb_convert_encoding( sprintf("\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"",
			$row['shop_id'],
			$row['type'],
			$row['shop_name'],
			$row['style'],
			$row['refer_price'],
			$row['cost_price'],
			$row['multiple'],
			$row['num'],
			$row['refer_price']*$row['num']*$row['multiple'],
			$row['s_unit'],
			$row['r_date'],
			$row['u_date'],
			$row['applicant'],
			$row['remark'],
			$row['special_reason'],
			$row['price_no'],
			$row['consumer_id'],
			$row['m_date'],
			$row['pro_name'],
			$row['category'],
			$row['producer'],
			$row['examiner'],
			$row['e_date'],
			$row['status'],
			$row['staff_id']
		),
		"Shift_JIS",
		"utf-8"
	);
}

$filename = date("YmdHis") . '.csv';

$head = mb_convert_encoding( $head, "Shift_JIS", "utf-8");


header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename=' . $filename );

print $head;
print "\n";
print implode( $data, "\n" );
*/
?>